package cn.chenxing.module.analysis.service;

import cn.chenxing.domain.dbscan.Point;
import cn.chenxing.domain.dto.TrackTable;
import cn.chenxing.domain.outlierdtect.DataNode;
import cn.chenxing.module.analysis.dbscan.DbScan;
import cn.chenxing.module.analysis.outlierdetect.OutlierDetect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 分析模块服务层
 * @Author maogen.ymg
 * @Date 2020/4/16 22:31
 */
@Service
@Slf4j
public class AnalysisService {

    /**
     * 离群点检测
     * @return result
     */
    public List<DataNode> outlierDetect(List<TrackTable> trackTableList, double errorValue) {
        OutlierDetect outlierDetect = new OutlierDetect();

        List<DataNode> points = new ArrayList<>();
        int i = 1;
        for(TrackTable trackTable : trackTableList) {
            // 配置数据
            double[] dimension = new double[2];
            dimension[0] = trackTable.getLongitude();
            dimension[1] = trackTable.getLatitude();

            DataNode dataNode = new DataNode(i++, dimension);
            dataNode.setId(trackTable.getId())
                    .setLongitude(trackTable.getLongitude())
                    .setLatitude(trackTable.getLatitude())
                    .setTime(trackTable.getTime())
                    .setSpeed(trackTable.getSpeed());
            points.add(dataNode);
        }

        return outlierDetect.getOutlierNode(points, errorValue);
    }

    public List<Point> dbScanCluster(List<TrackTable> trackTableList, int minPts, double eps) {
        DbScan dbScan = new DbScan(minPts, eps);
        List<Point> points = new ArrayList<>();

        int id = 0;
        for(TrackTable trackTable : trackTableList) {
            // 配置数据
            Point point = new Point(trackTable.getLongitude(), trackTable.getLatitude(), trackTable.getId());
            points.add(point);
        }
        // 处理
        dbScan.process(points);

        return points;
    }

}
